package com.guanji.learn.service;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Upload;
import com.amazonaws.services.s3.transfer.model.UploadResult;

import java.io.File;

public class OssUploadService {

    public static void main(String[] args) throws InterruptedException {
        AWSCredentials credentials = new BasicAWSCredentials("32863946112", "746ed15c1ef24bae0f26650412897f5e");
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("https://fusionbiad-test-32863946112.hz.s3down.com", "hz"))
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .build();
        // 上传(自动分片)
        // https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/transfer/TransferManager.html
        TransferManager tm = TransferManagerBuilder.standard()
                .withS3Client(s3Client)
                .withMinimumUploadPartSize(8 * 1024 * 1024L)
                .withMultipartUploadThreshold(8 * 1024 * 1024L)
                .build();
        Upload upload = tm.upload("fusionbiad-test-32863946112", "test/fA37fIKCz3.png", new File("C:\\Users\\Administrator\\Downloads\\fA37fIKCz.png"));
        upload.waitForCompletion();
        UploadResult uploadResult = upload.waitForUploadResult();
        System.out.println("上传完成");
    }

}
